﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace AnaViewer
{
    /// <summary>
    /// 提供私有字体集合等功能。
    /// </summary>
    public class FontsHelper
    {
        /// <summary>
        /// 字体文件的后缀名。
        /// </summary>
        static List<string> fontExtensions = new List<string>(new string[] { "*.fon", "*.ttf", "*.t1" });//T1 为自定义的字体后缀
        /// <summary>
        /// 存放字体文件的文件相对路径（相对于可执行程序）。
        /// </summary>
        const string FontPath = "resource";//"fonts"
        static string FontFullPath
        {
            get {return  Path.Combine(Application.StartupPath, FontPath); }
        }


        /// <summary>
        /// 尝试在 已安装字体和私有字体集合 中创建 FontFamily 实例。
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static FontFamily GetFamily(string name)
        {
            FontFamily family = null;
            try
            {
                family = new FontFamily(name);//InstalledFontCollection
            }
            catch (ArgumentException)
            {//系统字体中不存在，则使用私有字体
            }

            if (family == null)
            {
                try
                {
                    family = new FontFamily(name, FontsHelper.FontCollection);
                }
                catch (ArgumentException)
                {//系统字体中不存在，则使用私有字体
                }
            }

            if (family == null)
                family = FontFamily.GenericSansSerif;

            return family;
        }


        static PrivateFontCollection privateFontCollection;
        static FontFamily[] fontFamilies;
        public static FontCollection FontCollection
        {
            get { return privateFontCollection; }
        }
        public static FontFamily[] FontFamilies
        {
            get { return fontFamilies; }
        }
        static FontsHelper()
        {
            privateFontCollection = new PrivateFontCollection();
            string fontPath = FontFullPath;
            if (Directory.Exists(fontPath))
            {//从目录中加载字体
                //对于每种字体扩展名
                foreach (string fontExtension in fontExtensions)
                    foreach (string filename in Directory.GetFiles(fontPath,fontExtension))
                    {
                        try
                        {
                            privateFontCollection.AddFontFile(filename);
                        }
                        catch (FileNotFoundException)
                        { }
                    }
            }

            fontFamilies = privateFontCollection.Families;
        }


        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
        //    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //    PointF pointF = new PointF(10, 0);
        //    SolidBrush solidBrush = new SolidBrush(Color.Black);

        //    int count = 0;
        //    string familyName = "";
        //    string familyNameAndStyle;

        //    // How many objects in the fontFamilies array?
        //    count = fontFamilies.Length;

        //    // Display the name of each font family in the private collection
        //    // along with the available styles for that font family.
        //    for (int j = 0; j < count; ++j)
        //    {
        //        // Get the font family name.
        //        familyName = fontFamilies[j].Name;

        //        // Is the regular style available?
        //        if (fontFamilies[j].IsStyleAvailable(FontStyle.Regular))
        //        {
        //            familyNameAndStyle = "";
        //            familyNameAndStyle = familyNameAndStyle + familyName;
        //            familyNameAndStyle = familyNameAndStyle + " Regular";

        //            Font regFont = new Font(
        //               familyName,
        //               16,
        //               FontStyle.Regular,
        //               GraphicsUnit.Pixel);

        //            e.Graphics.DrawString(
        //               familyNameAndStyle,
        //               regFont,
        //               solidBrush,
        //               pointF);

        //            pointF.Y += regFont.Height;
        //        }

        //        // Is the bold style available?
        //        if (fontFamilies[j].IsStyleAvailable(FontStyle.Bold))
        //        {
        //            familyNameAndStyle = "";
        //            familyNameAndStyle = familyNameAndStyle + familyName;
        //            familyNameAndStyle = familyNameAndStyle + " Bold";

        //            Font boldFont = new Font(
        //               familyName,
        //               16,
        //               FontStyle.Bold,
        //               GraphicsUnit.Pixel);

        //            e.Graphics.DrawString(familyNameAndStyle, boldFont, solidBrush, pointF);

        //            pointF.Y += boldFont.Height;
        //        }
        //        // Is the italic style available?
        //        if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic))
        //        {
        //            familyNameAndStyle = "";
        //            familyNameAndStyle = familyNameAndStyle + familyName;
        //            familyNameAndStyle = familyNameAndStyle + " Italic";

        //            Font italicFont = new Font(
        //               familyName,
        //               16,
        //               FontStyle.Italic,
        //               GraphicsUnit.Pixel);

        //            e.Graphics.DrawString(
        //               familyNameAndStyle,
        //               italicFont,
        //               solidBrush,
        //               pointF);

        //            pointF.Y += italicFont.Height;
        //        }

        //        // Is the bold italic style available?
        //        if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic) &&
        //        fontFamilies[j].IsStyleAvailable(FontStyle.Bold))
        //        {
        //            familyNameAndStyle = "";
        //            familyNameAndStyle = familyNameAndStyle + familyName;
        //            familyNameAndStyle = familyNameAndStyle + "BoldItalic";

        //            Font italicFont = new Font(
        //               familyName,
        //               16,
        //               FontStyle.Italic | FontStyle.Bold,
        //               GraphicsUnit.Pixel);

        //            e.Graphics.DrawString(
        //               familyNameAndStyle,
        //               italicFont,
        //               solidBrush,
        //               pointF);

        //            pointF.Y += italicFont.Height;
        //        }
        //        // Is the underline style available?
        //        if (fontFamilies[j].IsStyleAvailable(FontStyle.Underline))
        //        {
        //            familyNameAndStyle = "";
        //            familyNameAndStyle = familyNameAndStyle + familyName;
        //            familyNameAndStyle = familyNameAndStyle + " Underline";

        //            Font underlineFont = new Font(
        //               familyName,
        //               16,
        //               FontStyle.Underline,
        //               GraphicsUnit.Pixel);

        //            e.Graphics.DrawString(
        //               familyNameAndStyle,
        //               underlineFont,
        //               solidBrush,
        //               pointF);

        //            pointF.Y += underlineFont.Height;
        //        }

        //        // Is the strikeout style available?
        //        if (fontFamilies[j].IsStyleAvailable(FontStyle.Strikeout))
        //        {
        //            familyNameAndStyle = "";
        //            familyNameAndStyle = familyNameAndStyle + familyName;
        //            familyNameAndStyle = familyNameAndStyle + " Strikeout";

        //            Font strikeFont = new Font(
        //               familyName,
        //               16,
        //               FontStyle.Strikeout,
        //               GraphicsUnit.Pixel);

        //            e.Graphics.DrawString(
        //               familyNameAndStyle,
        //               strikeFont,
        //               solidBrush,
        //               pointF);

        //            pointF.Y += strikeFont.Height;
        //        }

        //        // Separate the families with white space.
        //        pointF.Y += 10;

        //    } // for

        //    base.OnPaint(e);
        //}

    }
}
